package com.yanqu.road.server.cmd.patrons;

import com.yanqu.road.logic.pb.PatronsPb;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.pb.patrons.PatronsProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;

@Cmd(code = Protocol.S_PATRONS_BREAK, desc = "门客突破")
public class PatronsBreakCmd extends AbstractCommand {

	int patronsId;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		PatronsProto.PatronsBreakReqMsg reqMsg = PatronsProto.PatronsBreakReqMsg.parseFrom(packet.getBytes());
		patronsId = reqMsg.getPatronsId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		ret = player.getModule(PatronsModule.class).patronsBreak(patronsId);
	}

	@Override
	public void sendData(GamePlayer player) {
		PatronsProto.PatronsBreakRespMsg.Builder respMsg = PatronsProto.PatronsBreakRespMsg.newBuilder();
		respMsg.setRet(ret);
		if(0 == ret){
			UserPatrons userPatrons = player.getModule(PatronsModule.class).getUserPatrons(patronsId);
			PatronsProto.PatronsTempMsg.Builder patronsMsg = PatronsPb.parsePatronsTempMsg(userPatrons);
			if(null != patronsMsg) {
				respMsg.setPatrons(patronsMsg);
			}
		}
		player.sendPacket(Protocol.U_PATRONS_BREAK, respMsg);
	}
}
